package com.zsck.pos.view;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chinaums.commoncheck.R;
import com.zsck.pos.adapter.PopPayTypeAdapter;
import com.zsck.pos.adapter.PopProjectAdapter;
import com.zsck.pos.bean.Project;

import java.util.Arrays;
import java.util.List;

/**
 * 创建者     Bowllboy
 * 创建时间   2020/4/29 14:53
 * 描述	      pop管理
 * TODO 此处应优化处理 动态添加item 当前写死
 */
public class PopManager {
    private static PopManager mPopManager;
    //private Activity mContext;
    private PopupWindow mPop;

    public PopManager() {
        //this.mContext = context;
    }

    public static PopManager getInstance() {
        if (mPopManager == null) {
            synchronized (PopManager.class) {
                if (mPopManager == null) {
                    mPopManager = new PopManager();

                }
            }
        }
        return mPopManager;
    }

    public void showBottomPopWindow(Activity context, List<String> items, OnItemClick onItemClick) {
        View bottomView = View.inflate(context, R.layout.item_pop, null);
        RecyclerView recyclerView = bottomView.findViewById(R.id.pop_list);
        PopPayTypeAdapter popPayTypeAdapter = new PopPayTypeAdapter(items);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(popPayTypeAdapter);
        mPop = new PopupWindow(bottomView, -1, -2);
        mPop.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPop.setOutsideTouchable(true);
        mPop.setFocusable(true);
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = 0.5f;
        context.getWindow().setAttributes(lp);
        mPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = context.getWindow().getAttributes();
                lp.alpha = 1f;
                context.getWindow().setAttributes(lp);
            }
        });
        popPayTypeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                closePopupWindow();
                if (onItemClick != null) {
                    onItemClick.onItemClick(position);
                }
            }
        });
        mPop.setAnimationStyle(R.style.Bottom_in_out_anim);
        mPop.showAtLocation(context.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }

    public void showBottomProject(Activity context, List<Project> items, OnItemClick onItemClick) {
        View bottomView = View.inflate(context, R.layout.item_project_pop, null);
        RecyclerView recyclerView = bottomView.findViewById(R.id.pop_list);
        PopProjectAdapter popProjectAdapter = new PopProjectAdapter(items);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(popProjectAdapter);
        mPop = new PopupWindow(bottomView, -1, -2);
        mPop.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPop.setOutsideTouchable(true);
        mPop.setFocusable(true);
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = 0.5f;
        context.getWindow().setAttributes(lp);
        mPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = context.getWindow().getAttributes();
                lp.alpha = 1f;
                context.getWindow().setAttributes(lp);
            }
        });
        popProjectAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                closePopupWindow();
                if (onItemClick != null) {
                    onItemClick.onItemClick(position);
                }
            }
        });
        mPop.setAnimationStyle(R.style.Bottom_in_out_anim);
        mPop.showAtLocation(context.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }


    public void closePopupWindow() {
        if (mPop != null && mPop.isShowing()) {
            mPop.dismiss();
            mPop = null;
            mPopManager = null;
        }
    }

    public interface OnItemClick {
        void onItemClick(int position);

        void setCancelIitemClick();
    }


}
